using Game.Const;
using Game.Core;
using Game.Main.Model;
using Game.Main.View;
using Game.Manager;
using UnityEngine;

namespace Game.Component
{
	public class InfusionSupportItem : BaseItem
	{
		private InfusionBraceletItem braceletItem;

		private InfusionBagItem bagItem;

		public override void Init(ItemArchiveData data)
		{
			base.Init(data);
			Transform transform = model.Find("Pipe");
			InitComponentTrigger(transform.gameObject, ItemComponentTrigger.Type.InfusionBagBox);
		}

		protected void InitComponentTrigger(GameObject obj, ItemComponentTrigger.Type type)
		{
			obj.layer = 12;
			ItemComponentTrigger itemComponentTrigger = obj.AddComponent<ItemComponentTrigger>();
			itemComponentTrigger.Init(this, type, null, null, null);
		}

		public override bool AttachItem(BaseItem item, DepthType depthType, AttachType attachType, bool updatePosition)
		{
			bool flag = base.AttachItem(item, depthType, attachType, updatePosition);
			if (flag && item.itemData.scriptType == 160)
			{
				item.SetAllCollidersEnabled(false);
				bagItem = (InfusionBagItem)item;
			}
			return flag;
		}

		public override void ReleaseAttachItem(BaseItem item)
		{
			base.ReleaseAttachItem(item);
			if (item != null && item.itemData.scriptType == 160)
			{
				item.SetAllCollidersEnabled(true);
				if (bagItem == item)
				{
					bagItem = null;
				}
			}
		}

		public bool ChangeInfusionBag(BaseItem item)
		{
			BaseItem infusionBag = GetInfusionBag();
			if (infusionBag != null)
			{
				ReleaseAttachItem(infusionBag);
				infusionBag.DropDown(new Vector3(infusionBag.transform.position.x, base.transform.position.y, infusionBag.transform.position.z));
			}
			if (AttachItem(item, DepthType.Front, AttachType.PlacePoint, true))
			{
				Singleton<AudioManager>.Instance.PlaySound("sound_explode");
				Singleton<EffectManager>.Instance.ShowEffect(PathConst.EFFECT + "efc_explode_1", item.transform.position + new Vector3(0f, 0.4f, -0.0004f), 2f);
				Singleton<EffectManager>.Instance.ShowEffect(PathConst.EFFECT + "efc_blink_1", item.transform.position + new Vector3(0f, 0.4f, -0.0004f), 3f);
				return true;
			}
			return false;
		}

		private BaseItem GetInfusionBag()
		{
			for (int i = 0; i < childItems.Count; i++)
			{
				if (childItems[i].itemData.scriptType == 160)
				{
					return childItems[i];
				}
			}
			return null;
		}

		public void MinusBagFill(float val)
		{
			if (bagItem != null)
			{
				bagItem.MinusFillAmount(val);
			}
		}

		public override void SetLayer(int layer, int mapId)
		{
			base.SetLayer(layer, mapId);
			if (braceletItem == null)
			{
				BaseItem itemById = Singleton<Scene>.Instance.GetItemById(base.itemData.scriptParams);
				if (itemById != null)
				{
					braceletItem = (InfusionBraceletItem)itemById;
				}
			}
			if (braceletItem != null)
			{
				if (layer == 9)
				{
					braceletItem.SetCanAttach(false);
				}
				else
				{
					braceletItem.SetCanAttach(true);
				}
			}
		}

		public override void SetChildsColliderEnabled(bool val)
		{
			for (int i = 0; i < childItems.Count; i++)
			{
				BaseItem baseItem = childItems[i];
				if (baseItem != null && baseItem.itemData.scriptType != 160)
				{
					baseItem.SetAllCollidersEnabled(val);
				}
			}
		}
	}
}
